//
//  FeaturedCollectionsCollectionViewCell.m
//  Beauty
//
//  Created by Snow on 16/8/31.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "FeaturedCollectionsCollectionViewCell.h"
#import "GXMacro.h"
#import "ModelOfGoodList.h"
@interface FeaturedCollectionsCollectionViewCell ()
@property (nonatomic, strong) UIButton *button_1;
@property (nonatomic, strong) UIButton *button_2;
@property (nonatomic, strong) UIButton *button_3;
@property (nonatomic, strong) UIButton *button_4;
@property (nonatomic, strong) UILabel *label_1;
@property (nonatomic, strong) UILabel *label_2;
@property (nonatomic, strong) UILabel *label_3;
@property (nonatomic, strong) UILabel *label_4;

@end
@implementation FeaturedCollectionsCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageOfGoods = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageOfGoods];
        _button_1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button_1];
        _button_2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button_2];
        _button_3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button_3];
        _button_4 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_button_4];
        _label_1 = [[UILabel alloc] init];
        [self.contentView addSubview:_label_1];
        _label_2 = [[UILabel alloc] init];
        [self.contentView addSubview:_label_2];
        _label_3 = [[UILabel alloc] init];
        [self.contentView addSubview:_label_3];
        _label_4 = [[UILabel alloc] init];
        [self.contentView addSubview:_label_4];
        _button_1.tag = 0;
        _button_2.tag = 1;
        _button_3.tag = 2;
        _button_4.tag = 3;
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [_imageOfGoods mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.height.mas_equalTo(self.contentView.mas_height).multipliedBy(0.66);
    }];
    [_button_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageOfGoods.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(self.contentView.mas_left).mas_equalTo(5);
        make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.215);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-30);
    }];
    [_button_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageOfGoods.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(_button_1.mas_right).mas_equalTo(10);
        make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.215);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-30);
    }];
    [_button_3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageOfGoods.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(_button_2.mas_right).mas_equalTo(10);
        make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.215);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-30);
    }];
    [_button_4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageOfGoods.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(_button_3.mas_right).mas_equalTo(10);
        make.width.mas_equalTo(self.contentView.mas_width).multipliedBy(0.215);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-30);
    }];
    [_label_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_button_1.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(_button_1.mas_left);
        make.right.mas_equalTo(_button_1.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-10);
    }];
    [_label_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_button_2.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(_button_2.mas_left);
        make.right.mas_equalTo(_button_2.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-10);
    }];
    [_label_3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_button_3.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(_button_3.mas_left);
        make.right.mas_equalTo(_button_3.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-10);
    }];
    [_label_4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_button_4.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(_button_4.mas_left);
        make.right.mas_equalTo(_button_4.mas_right);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_equalTo(-10);
    }];
    
}
- (void)setArrayOfImages:(NSArray *)arrayOfImages
{
    _arrayOfImages = arrayOfImages;
    if (_arrayOfImages.count != 0) {
        ModelOfGoodList *model1 = [_arrayOfImages firstObject];
        ModelOfGoodList *model2 = [_arrayOfImages objectAtIndex:1];
        ModelOfGoodList *model3 = [_arrayOfImages objectAtIndex:2];
        ModelOfGoodList *model4 = [_arrayOfImages objectAtIndex:3];
        _label_1.textAlignment = 1;
        _label_3.textAlignment = 1;
        _label_2.textAlignment = 1;
        _label_4.textAlignment = 1;
        _label_1.font = [UIFont systemFontOfSize:14];
        _label_2.font = [UIFont systemFontOfSize:14];
        _label_3.font = [UIFont systemFontOfSize:14];
        _label_4.font = [UIFont systemFontOfSize:14];
        _label_1.text = [NSString stringWithFormat:@"￥ %@" , model1.goods_promotion_price];
        _label_2.text = [NSString stringWithFormat:@"￥ %@" , model2.goods_promotion_price];
        _label_3.text = [NSString stringWithFormat:@"￥ %@" , model3.goods_promotion_price];
        _label_4.text = [NSString stringWithFormat:@"￥ %@" , model4.goods_promotion_price];

        [_button_1 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model1.goods_image]]] forState:UIControlStateNormal];
        [_button_2 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model2.goods_image]]] forState:UIControlStateNormal];
        [_button_3 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model3.goods_image]]] forState:UIControlStateNormal];
        [_button_4 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model4.goods_image]]] forState:UIControlStateNormal];
        _imageOfGoods.layer.masksToBounds = YES;
        _imageOfGoods.layer.cornerRadius = 5;
        [_button_1 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
        [_button_2 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
        [_button_3 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
        [_button_4 addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)handleButton:(UIButton *)button
{
    NSInteger index = button.tag;
    ModelOfGoodList *model = [_arrayOfImages objectAtIndex:index];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(featuredWithGoodId:)]) {

        [self.delegate featuredWithGoodId:model.goods_id];
    }
}
@end
